package com.intel.two.service.older.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.older.LayuiPageVo;

import com.intel.two.mapper.older.PeopleMapper;
import com.intel.two.pojo.older.People;
import com.intel.two.service.older.PeopleService;
import com.intel.two.vo.older.BarVo;
import com.intel.two.vo.older.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service("peopleService")
public class PeopleServiceImpl implements PeopleService {

    @Autowired
    private PeopleMapper peopleMapper;


    @Override
    public LayuiPageVo<People> peoplePageData(Map<String, Object> params) {

        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<People> people =  peopleMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<People> peoplePageInfo = new PageInfo<>(people);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<People> peopleLayuiPageVo = new LayuiPageVo<>();
        peopleLayuiPageVo.setCode(0);
        peopleLayuiPageVo.setMsg("学生分页列表数据");
        peopleLayuiPageVo.setCount(peoplePageInfo.getTotal());  // 总记录数
        peopleLayuiPageVo.setData(peoplePageInfo.getList());  // 分页列表数据

        return peopleLayuiPageVo;
    }

    @Override
    public int addPeople(People people) {
        int result = peopleMapper.addPeople(people);
        return result;
    }

    @Override
    public int deleteById(Integer id) {
        System.out.println(id);
        int result = peopleMapper.deleteById(id);
        return result;
    }

    @Override
    public int deleteAll(Map<String, Object> map) {
        String str = (String) map.get("oldman_id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return peopleMapper.deleteAll(list);
    }

    @Override
    public int udatePeople(People people) {
        int result = peopleMapper.updatePeople(people);
        return result;
    }

    @Override
    public List<BarVo> sexBarData() {


        return peopleMapper.sexBarData();
    }

    @Override
    public List<PieVo> sexPieData() {
        return peopleMapper.sexPieData();
    }
}
